# Copyright (c) 2025 Huawei Technologies Co., Ltd.
# openUBMC is licensed under Mulan PSL v2.
# You can use this software according to the terms and conditions of the Mulan PSL v2.
# You may obtain a copy of Mulan PSL v2 at:
#         http://license.coscl.org.cn/MulanPSL2
# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
# EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
# MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
# See the Mulan PSL v2 for more details.
import os

from conans import ConanFile, tools, AutoToolsBuildEnvironment

class BisonConan(ConanFile):
    name = "bison"
    license = "GPL V3.0"
    description = "Bison is a general-purpose parser"
    chost = None
    settings = "os", "compiler", "build_type", "arch"
    _autotools = None
    
    def source(self):
        tools.get(**self.conan_data["sources"][self.version], strip_root=True)
    
    def _configure_autotools(self):
        if self._autotools:
            return self._autotools
        self._autotools = AutoToolsBuildEnvironment(self)
    
        self._autotools.configure()
        return self._autotools

    def build(self):
        autotools = self._configure_autotools()
        chost = os.environ.get("CHOST")
        if chost:
            os.environ["CROSS_COMPILE"] = chost + "-"
        autotools.make()
    
    def package(self):
        autotools = self._configure_autotools()
        autotools.install()

    def package_info(self):
        self.env_info.PATH.append(os.path.join(self.package_folder, "bin"))